package com.gopay.scheduler.service.wallet;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.cps.vo.CPSResponse;
import com.gopay.common.domain.cps.wallet.Wallet01609VO;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.JsonUtils;
import com.gopay.common.util.StringTools;
import com.gopay.cron.web.util.CallCPSUtil;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 *
 * Created by zhangjianlong on 2017/1/5. All Right reserved
 */
public class CatchBonusTask implements Runnable{

    // 日志
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private CountDownLatch latch;

    private List<Object[]> objectsList;

    public CatchBonusTask(CountDownLatch latch, List<Object[]> objectsList) {
        this.latch = latch;
        this.objectsList = objectsList;
    }

    @Override
    public void run() {
        for (int i = 0; i <objectsList.size() ; i++) {
            Object[] objects = objectsList.get(i);

            Wallet01609VO wallet01609VO = new Wallet01609VO();
            wallet01609VO.setOrder01609Id(StringTools.checkNulltoString(objects[0]));   //01609订单ID
            wallet01609VO.setInnerPayAccID(StringTools.checkNulltoString(objects[1]));  //中间账户
            wallet01609VO.setBounsDetailOrderId(StringTools.checkNulltoString(objects[2]));
            // 调用cps
            CPSResponse response = null;

            try {
                this.logger.info("需要入账的口令红包记录的订单号为:{},中间账户为:{},口令红包订单号为:{}",objects);
                response = CallCPSUtil.callByVOTicket(wallet01609VO);
                latch.countDown();
                logger.info("口令红包请求CPS做账请求结果：" +JsonUtils.toJson(response));
            } catch (JsonGenerationException e) {
                logger.error("口令红包请求CPS做账错误", e);
            } catch (JsonMappingException e) {
                logger.error("口令红包请求CPS做账错误", e);
            } catch (IOException e) {
                logger.error("口令红包请求CPS做账错误", e);
            } catch (Exception e) {
                // 吃掉所有异常
                logger.error("口令红包请求CPS做账错误", e);
            }
        }



    }
}
